package com.ujn.demo.use;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 拼音转九宫格拼音
 *
 * @author sunjunxian
 *
 */
public class PinyinToNumber {

    private static final Logger LOGGER = LoggerFactory.getLogger(PinyinToNumber.class);

    private static final String filePath = "C:\\Users\\可爱的小宝贝\\Desktop\\身份证\\0402";

    private static List<String> nameList = new ArrayList<>();

    private static List<String> pinyinNameList = new ArrayList<>();

    private static List<String> numList = new ArrayList<>();

    private static Map<String, String> numMap = new HashMap<String, String>();

    //数字对应字母
    static {
        numMap.put("a","2");
        numMap.put("b","2");
        numMap.put("c","2");
        numMap.put("d" ,"3");
        numMap.put("e" ,"3");
        numMap.put("f" ,"3");
        numMap.put("g" ,"4");
        numMap.put("h" ,"4");
        numMap.put("i" ,"4");
        numMap.put("j" ,"5");
        numMap.put("k" ,"5");
        numMap.put("l" ,"5");
        numMap.put("m" ,"6");
        numMap.put("n" ,"6");
        numMap.put("o" ,"6");
        numMap.put("p" ,"7");
        numMap.put("q" ,"7");
        numMap.put("r" ,"7");
        numMap.put("s" ,"7");
        numMap.put("t" ,"8");
        numMap.put("u" ,"8");
        numMap.put("v" ,"8");
        numMap.put("w" ,"9");
        numMap.put("x" ,"9");
        numMap.put("y" ,"9");
        numMap.put("z" ,"9");
        numMap.put(" " ," ");
    }

    public static void main(String[] args) throws IOException, InvalidFormatException {

        readExcel();
        writeExcel();

//        HashMap<String, String> map = new HashMap<>(32, 0.9f);
//        Set<Map.Entry<String, String>> entries = map.entrySet();
//        for (Map.Entry entry:entries) {
//            entry.getKey();
//            entry.getValue();
//        }

    }
    @Test
    public void test(){
//        Set<Integer> set = new HashSet<>();
//        set.add(1);
//        set.add(11);
//        set.add(21);
//        set.add(31);
//        set.add(41);
//        set.add(51);
//        Iterator iterator = set.iterator();
//        while (iterator.hasNext()){
//            Integer next = (Integer) iterator.next();
//            System.out.println(next);
//        }

        HashMap<String, String> map = new HashMap<>(8, 0.75f);
        map.put(12 + "", 12 + "");
        map.put(13 + "", 12 + "");
        map.put(14 + "", 12 + "");
        map.put(15 + "", 12 + "");
        map.put(16 + "", 12 + "");
        map.put(17 + "", 12 + "");
        map.put(18 + "", 12 + "");
        map.put(19 + "", 12 + "");
        map.put(10 + "", 12 + "");
        map.put(11 + "", 12 + "");
        map.put(102 + "", 12 + "");
        map.put(104 + "", 12 + "");
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry:entries) {
            System.out.println(entry.getKey() + ";" + entry.getValue());
        }



//        Hashtable<String, String> table = new Hashtable<>();
//        table.put(null, null);
//        Iterator iterator = map.entrySet().iterator();
//        while (iterator.hasNext()){
//            String next = (String) iterator.next();
//            System.out.println(next);
//        }
//
//        ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap<>();
//        concurrentHashMap.put("1", "2");
//
//        Vector<String> vector = new Vector<>();
//        vector.add("44");

        ArrayList<Object> list = new ArrayList<>();
        list.add(5, 9);
        list.remove(9);

        List<Object> objects = Collections.synchronizedList(new ArrayList<>());
        LinkedList<Object> linkedList = new LinkedList<>(objects);
        linkedList.add(0);
        System.out.println(list.get(0));
    }

    /**
     * 拼音转化为九宫格数字
     */
    public static void pinyinToNum(){
        for (String name : pinyinNameList) {
            String nameListNum = "";
            for (int i=0; i<name.length(); i++){
                char strChar = name.charAt(i);
                String str = strChar + "";
                nameListNum += numMap.get(str);
            }
            numList.add(nameListNum);
        }
    }

    /**
     * 写excel
     * @throws IOException
     */
    public static void writeExcel() throws IOException {
        try{
            //调用拼音转化为九宫格数字的方法
            pinyinToNum();
            // 创建工作薄
            HSSFWorkbook workbook = new HSSFWorkbook();
            // 创建工作表
            HSSFSheet sheet = workbook.createSheet("sheet1");

            //row 行数
            for (int row = 0; row < nameList.size(); row++) {
                HSSFRow rows = sheet.createRow(row);
                //clo 列数
                for (int col = 0; col < 3; col++) {
                    if (col == 0){
                        rows.createCell(col).setCellValue(nameList.get(row));
                    }else if (col == 1){
                        rows.createCell(col).setCellValue(pinyinNameList.get(row));
                    }else if (col == 2){
                        rows.createCell(col).setCellValue(numList.get(row));
                    }
                    // 向工作表中添加数据
//                rows.createCell(col).setCellValue("data" + row + col);
                }
            }
            String time = "/拼音转九宫格-" + getTime() + ".xls";
            File xlsFile = new File(filePath + time);
            xlsFile.createNewFile();
            FileOutputStream xlsStream = new FileOutputStream(xlsFile);
            workbook.write(xlsStream);
            LOGGER.info("writeExcel，拼音转化九宫格成功，请查看excel位置：{}", xlsFile.getAbsolutePath());
        }catch (Exception e){
            LOGGER.error("writeExcel，错误：{}", e);
        }
    }

    /**
     * 获取时间
     * @return
     */
    public static String getTime(){
        Date currentTime = new Date();
        //改变输出格式（自己想要的格式）
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
        //得到字符串时间
        String time = formatter.format(currentTime);
        return time;
    }

    /**
     * 读取excel
     * @throws IOException
     * @throws InvalidFormatException
     */
    public static void readExcel() throws IOException, InvalidFormatException {
        File xlsFile = new File(filePath + "/name.xls");
        // 获得工作簿
        Workbook workbook = WorkbookFactory.create(xlsFile);
        // 获得工作表个数
        int sheetCount = workbook.getNumberOfSheets();
        // 遍历工作表
        for (int i = 0; i < sheetCount; i++) {
            Sheet sheet = workbook.getSheetAt(i);
            // 获得行数
            int rows = sheet.getLastRowNum() + 1;
            // 获得列数，先获得一行，在得到改行列数
            Row tmp = sheet.getRow(0);
            if (tmp == null) {
                continue;
            }
            int cols = tmp.getPhysicalNumberOfCells();
            // 读取数据
            for (int row = 0; row < rows; row++) {
                //获取第几行
                Row r = sheet.getRow(row);
                for (int col = 0; col < cols; col++) {
                    if (col == 0){
                        nameList.add(r.getCell(col).getStringCellValue());
                    }else{
                        pinyinNameList.add(r.getCell(col).getStringCellValue());
                    }
//                    System.out.printf("%10s", r.getCell(col).getStringCellValue());
                }
            }
        }
    }

}